﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using System.Timers;

namespace kb_24_the_game
{
    internal class Level4
    {
        #region Variabelen Declaratie
        public bool reset = false;
        private bool teLaat = false, up = false, ingecheckt = false, bovencollision = false;
        private int timeLeft = 3000;
        private Biertje biertje;
        private Color kleur=Color.White;
        private ContentManager content;
        private Backgrounds scrollBackground1;
        private IList<ISprite> allSprites;
        private Player speler;
        //private Obstakel ovPaal, bank1, bank2, bank3, bank4;
        private List<ObstakelTest> obstakellijst = new List<ObstakelTest>();
        private List<ObstakelTest> bankLijst = new List<ObstakelTest>();     
        private Enemies conducteur;
        private SpriteFont font, fontreset;
        private Rectangle spelerRect;
        private KeyboardState state;
        private Rectangle oldCollisionRectangle;
        XML_IO xml = new XML_IO();

        #endregion Variabelen Declaratie

        public Level4(ContentManager content)
        { 
            this.content = content;  
            allSprites = new List<ISprite>();                     
            speler = new Player(content, new Vector2(50, 425));
            biertje = new Biertje(content, new Vector2(150, 400), 4);
            conducteur = new Enemies(content, 3, new Vector2(900, 403));
            obstakellijst = xml.readXML(@"XML/level4.xml");
            foreach (ObstakelTest obstakel in obstakellijst)
            {
                obstakel.content = content;
            }
            font = content.Load<SpriteFont>("AlgemeneTextures/tussenSchermTekst");
            fontreset = content.Load<SpriteFont>("AlgemeneTextures/fontReset");
            Load();
        }
        private void Load()
        {
            scrollBackground1 = new Backgrounds(content.Load<Texture2D>("Achtergronden/BackgroundLevel04"), new Rectangle(0, 0, 3500, 600));
            allSprites.Add(scrollBackground1);
            foreach (ObstakelTest obstakel in obstakellijst)
            {
                allSprites.Add(obstakel);
                if (obstakel.type == 8)
                {
                    bankLijst.Add(obstakel);
                }
            }
            allSprites.Add(conducteur);
            allSprites.Add(biertje);
            allSprites.Add(speler);
            
        }
        public bool Update(GameTime gameTime)
        {
            spelerRect = new Rectangle((int)speler.playerPosition.X, (int)speler.playerPosition.Y, speler.texture.Width / 4, speler.texture.Height / 2);
            timeLeft--; 
            CheckReset();
            speler.Springen(up);
            CheckKeys();
            CheckKleur();
            foreach (ObstakelTest obstakel in obstakellijst)
            {
                obstakel.Update(gameTime);
            }
            if (obstakellijst[0].colliderRect != null)
            {
                CheckCollisionsMetBiertjes(spelerRect);
                CheckCollisionsSpelerMetBank(spelerRect);
            }
            
            speler.BovenCollision(bovencollision);
            biertje.CheckPositionsSpelerBiertje(speler.playerPosition);
            speler.Update(gameTime);
            CheckCollisionsSpelerMetConducteur(spelerRect);
            conducteur.SetSpelerDirectie(speler.loopDirectie);
            conducteur.Update(gameTime);

            return CheckEindCondities();
        }
        private void CheckKeys()
        {
            state = Keyboard.GetState();
            if (state.IsKeyDown(Keys.Z))
            {
                biertje.aantalBiertjes = 5;
            }
            if (!teLaat)
            {
                if (state.IsKeyDown(Keys.Up))
                {
                    up = true;
                }
                else
                {
                    up = false;
                }
                if (state.IsKeyDown(Keys.Left))
                {
                    if (scrollBackground1.rectangle.X <= -1)
                    {
                        scrollBackground1.Links();
                    }
                    if (speler.playerPosition.X >= 50)
                    {
                        speler.Links();
                        biertje.BeweegLinks();
                        foreach (ObstakelTest o in obstakellijst)
                        {
                            o.BeweegLinks();
                        }
                    }
                }
                if (state.IsKeyDown(Keys.Right))
                {
                    if (scrollBackground1.rectangle.X >= -2699)
                    {
                        scrollBackground1.Rechts();
                    }
                    if (speler.playerPosition.X <= 500)
                    {
                        speler.Rechts();
                        biertje.BeweegRechts(); 
                        foreach (ObstakelTest o in obstakellijst)
                        {
                            o.BeweegRechts();
                        }
                    }
                }
            }
        }
        public void Draw(SpriteBatch spriteBatch)
        {
            foreach (ISprite sprite in allSprites)
            {
                sprite.Draw(spriteBatch);
            }
            if (obstakellijst[0].colliderRect != null)
            {
                if (CheckCollisionsMetOVpaal(spelerRect) && biertje.aantalBiertjes >= 5 && !ingecheckt)
                {
                    spriteBatch.DrawString(font, "Druk op Spatie om in te checken.", new Vector2(scrollBackground1.rectangle.X + scrollBackground1.rectangle.Width - 1700, 250), Color.White);
                }
                if (CheckCollisionsMetDeur(spelerRect) && biertje.aantalBiertjes >= 5 && ingecheckt)
                {
                    spriteBatch.DrawString(font, "Druk op Spatie om de trein in te stappen.", new Vector2(scrollBackground1.rectangle.X + scrollBackground1.rectangle.Width - 600, 250), Color.White);
                }
                if (timeLeft / 60 >= 0)
                {
                    spriteBatch.DrawString(font, "Tijd tot de trein vertrekt: " + timeLeft / 60 + " seconden", new Vector2(30, 100), kleur);
                    if (!ingecheckt && biertje.aantalBiertjes >= 5)
                    {
                        spriteBatch.DrawString(font, "Vergeet niet in te checken!", new Vector2(30, 125), kleur);
                    }
                    else if (ingecheckt && biertje.aantalBiertjes >= 5)
                    {
                        spriteBatch.DrawString(font, "Haast je naar de deur!", new Vector2(30, 125), kleur);
                    }
                }
                else if (timeLeft / 60 <= 0)
                {
                    teLaat = true;
                    spriteBatch.DrawString(fontreset, "Je hebt de trein gemist! ", new Vector2(25, 200), Color.Red);
                    spriteBatch.DrawString(font, "Druk op spate om opnieuw te beginnen", new Vector2(200, 300), Color.White);
                }
            }
        }
        private void CheckCollisionsMetBiertjes(Rectangle spelerRect)
        {
            Rectangle bier = new Rectangle((int)biertje.position.X, (int)biertje.position.Y, biertje.texture.Width, biertje.texture.Height);
            Rectangle overlap = Rectangle.Intersect(spelerRect, bier);
            if (!overlap.IsEmpty)
            {
                biertje.Geraakt(scrollBackground1.rectangle.X, scrollBackground1.rectangle.Width);
                timeLeft = timeLeft + 300;
            }
        }
        private bool CheckCollisionsMetDeur(Rectangle spelerRect)
        {
            Rectangle deur = new Rectangle(scrollBackground1.rectangle.X + scrollBackground1.rectangle.Width - 330, 383, 68, 104);
            Rectangle overlap = Rectangle.Intersect(spelerRect, deur);
            if (!overlap.IsEmpty)
            {
                return true;
            }
            return false;
        }
        private bool CheckCollisionsMetOVpaal(Rectangle spelerRect)
        {
            Rectangle paal = new Rectangle((int)obstakellijst[0].position.X, (int)obstakellijst[0].position.Y, obstakellijst[0].texture.Width, obstakellijst[0].texture.Height);
            Rectangle overlap = Rectangle.Intersect(spelerRect, paal);
            if (!overlap.IsEmpty)
            {
                return true;
            }
            return false;
        }

        private bool CheckEindCondities()
        {
            if (CheckCollisionsMetOVpaal(spelerRect))
            {
                if (state.IsKeyDown(Keys.Space) && biertje.aantalBiertjes >= 5 && !ingecheckt)
                {

                    timeLeft = timeLeft + 300;
                    ingecheckt = true;
                    return false;
                }
            }
            if (CheckCollisionsMetDeur(spelerRect))
            {
                if (state.IsKeyDown(Keys.Space) && biertje.aantalBiertjes >= 5 && ingecheckt)
                {
                    return true;
                }
            }
            return false;
        }
        public void CheckReset()
        {
            if (state.IsKeyDown(Keys.Space) && timeLeft / 60 <= 0)
            {
                reset = true;
            }
        }
        public void CheckKleur()
        {
            if (timeLeft / 60 >= 5)
            {
                kleur = Color.White;
            }
            else
            {
                kleur = Color.Red;
            }
        }
        private void CheckCollisionsSpelerMetConducteur(Rectangle spelerRect)
        {
                Rectangle rect1 = new Rectangle((int)conducteur.enemyPosition.X, (int)conducteur.enemyPosition.Y, conducteur.texture.Width, conducteur.texture.Height/2);
                Rectangle overlap = Rectangle.Intersect(spelerRect, rect1);
                
                if (!overlap.IsEmpty && oldCollisionRectangle.IsEmpty)
                {
                    biertje.ResetBiertje(scrollBackground1.rectangle.X, scrollBackground1.rectangle.Width);
                    timeLeft = timeLeft - 600;
                    ingecheckt = false;
                }
                oldCollisionRectangle = overlap;  
        }
        private void CheckCollisionsSpelerMetBank(Rectangle spelerRect)
        {
            bovencollision = false;
            foreach (ObstakelTest bank in bankLijst)
            {
                foreach (Rectangle r in bank.colliderRect)
                {
                    Rectangle overlap = Rectangle.Intersect(spelerRect, r);

                    if (!overlap.IsEmpty && overlap.Height < 10 && overlap.Width > 20)
                    {
                        bovencollision = true;
                    }
                }
            }
        }
    }
}
